﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Business.MailTemplate;
using System.Globalization;
using System.Threading;
using System.Configuration;

namespace blackhouse.BlackApart.Web.Mails {
    public partial class HtmlMailGenerator : System.Web.UI.Page {

        #region Fields

        private string skin = "HtmlMail";
        private int reservationId = 0;
        private int templateId = 0;
        private Guid reservationGuid = Guid.Empty;
        private IWebReservation reservation;
        private IMailTemplate template;
        private MailTemplateFile templateFile;
        private Factory factory = Factory.Instance();

        #endregion

        #region Event Handlers

        protected override void OnPreInit(EventArgs e) {
            this.ReadParameters();
            this.LoadMasterPage();

            base.OnPreInit(e);
        }

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            if (!this.SetInstance())
                Response.End();
            this.MakeLocalTemplate();
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.PopulateTemplate();
        }

        #endregion

        #region Methods

        private void PopulateTemplate() {
            HtmlMailTemplate templateControl = this.LoadControl(this.templateFile.GetControlFilePath(this.template)) as HtmlMailTemplate;
            if (templateControl != null) {
                templateControl.Reservation = this.reservation;
                this.pnlMain.Controls.Add(templateControl);
            }
        }

        private void ReadParameters() {
            if (!string.IsNullOrEmpty(Request["skin"]) && this.CheckSkinName(Request["skin"]))
                this.skin = Request["skin"];
            Int32.TryParse(Request["reservationId"], out this.reservationId);
            Int32.TryParse(Request["templateId"], out this.templateId);
            Guid.TryParse(Request["guid"], out this.reservationGuid);
        }

        private bool CheckSkinName(string skin) {
            return new FileInfo(Server.MapPath("~/Mails/" + skin + ".Master")).Exists;
        }

        private bool SetInstance() {
            this.reservation = factory.GetWebReservation(this.reservationId);
            if (!this.reservation.Guid.Equals(reservationGuid)) {
                this.reservation = null;
                return false;
            }
            this.template = factory.GetMailTemplate(this.templateId);
            return true;
        }

        private void MakeLocalTemplate() {
            this.templateFile = new MailTemplateFile(new DirectoryInfo(Server.MapPath("~/Mails/TempTemplates/")), new DirectoryInfo(Server.MapPath("~/")), CultureInfo.CurrentCulture.Name);
            templateFile.CreateOrUpdateIfNewest(this.template);
        }

        private void LoadMasterPage() {
            this.MasterPageFile = "~/Mails/" + this.skin + ".Master";
        }

        protected override void InitializeCulture() {
            if (!string.IsNullOrEmpty(Request.QueryString["siteId"])) {
                int siteId = 0;
                if (Int32.TryParse(Request.QueryString["siteId"], out siteId)) {
                    string siteCulture = ConfigurationManager.AppSettings["site" + siteId + "culture"];
                    if (!string.IsNullOrEmpty(siteCulture)) {
                        UICulture = siteCulture;
                        Culture = siteCulture;

                        Thread.CurrentThread.CurrentCulture =
                            CultureInfo.CreateSpecificCulture(siteCulture);
                        Thread.CurrentThread.CurrentUICulture = new
                            CultureInfo(siteCulture);
                    }
                }
            }
            else if (!string.IsNullOrEmpty(Request.QueryString["language"])) {
                string selectedLanguage = Request.QueryString["language"];
                UICulture = selectedLanguage;
                Culture = selectedLanguage;

                Thread.CurrentThread.CurrentCulture =
                    CultureInfo.CreateSpecificCulture(selectedLanguage);
                Thread.CurrentThread.CurrentUICulture = new
                    CultureInfo(selectedLanguage);
            }
            base.InitializeCulture();

        }

        #endregion

    }
}